var co = require('co');
var seqModule = require('../../models/index');
var db = new seqModule();
var util = require('../../common/util');
var meetScheduleController = {
    getSchedulePage: function (req, res) {
        co(function () {
            var page = parseInt(req.query.page);
            var params = {};
            var paramT = {};
            var include = [{model:db.buildModule,attributes:['buildTitle']},{model:db.meetSubjectModule,attributes:['subjectTitle']},{model:db.speakHostModule,attributes:['name','headImg','tel','hospitalName']}];
            paramT.include = include;
            if (req.query.scheduleTitle != undefined) {
                params["scheduleTitle"] = { "$like": "%" + req.query.scheduleTitle + "%" };
            }
            if (req.query.buildId != undefined) {
                params["buildId"] = req.query.buildId;
            }
            if (req.query.meetScheduleId != undefined) {
                params["meetScheduleId"] = req.query.meetScheduleId;
            }
            if (req.query.type != undefined) {
                params["type"] = parseInt(req.query.type);
            }
            paramT["where"] = params;
            paramT["order"] = [["createTime", 'DESC']];
            if (req.query.page != undefined && req.query.rows != undefined) {
                paramT["offset"] = parseInt(req.query.page);
                paramT["limit"] = parseInt(req.query.rows);
            }
            db.meetScheduleModule.findAndCountAll(paramT).then(function (result) {
                result.page = page;
                res.status(200).json(result);
            });
        }).catch(function (e) {
            res.status(500).json({"message":"服务异常！"});
            console.log(e);
        })
    },  
    getScheduleById: function (req, res) {
        co(function () {
            var paramT = {};
            paramT.where = req.params;
            db.meetScheduleModule.findOne(paramT).then(function (result) {
                res.status(200).json(result);
            });
        }).catch(function (e) {
            res.status(500).json({"message":"服务异常！"});
            console.log(e);
        })
    },  
    addSchedule: function (req, res) {
        co(function () {
            req.body.id = util.getUUID();
            req.body.createTime = util.getNowTime();
            db.meetScheduleModule.create(req.body).then(function (result) {
                res.status(200).json(result);
            });
        }).catch(function (e) {
            res.status(500).json({"message":"服务异常！"});
            console.log(e);
        })
    },
    updateSchedule: function (req, res) {
        co(function () {
          var paramT = {};
          paramT.where = req.params;
            db.meetScheduleModule.update(req.body,paramT).then(function (result) {
                res.status(200).json(result);
            });
        }).catch(function (e) {
            res.status(500).json({"message":"服务异常！"});
            console.log(e);
        })
    },
    delScheduleByIds: function (req, res) {
        co(function () {
            var paramT = {};
            var paramU = {};
            var StringId = req.query.params;//传过来的params是以‘,’隔开的字符串
            var arr = StringId.split(",");
            paramU['id'] = [arr];
            paramT['where'] = paramU;
            //destroy、方法就是删除记录的方法 paramT是删除条件
            db.meetScheduleModule.destroy(paramT).then(function (results) {
                res.status(200).json(results);
            });
        }).catch(function (e) {
            res.status(500).json({"message":"服务异常！"});
            console.log(e);
        })
    }
}
module.exports = meetScheduleController;